﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<eCRAM.WebSite.Models.ValidateCramByDateModel>>" %>

<%@ Import Namespace="eCRAM.Model" %>
<%@ Import Namespace="eCRAM.Tools" %>
<%@ Import Namespace="eCRAM.WebSite.Tools" %>
<%@ Import Namespace="System.Globalization" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    eCram - Liste des crams à valider
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Liste des crams à valider</h2>
    <form id="CramList" method="post">
    <%
        if (!Model.Any())
        {
            Response.Write("Il n'y a aucun cram à valider");
        }

        Model.OrderBy(i => i.Year).ThenBy(i => i.Month);%>
    <%=Html.Hidden("cramLineId", "")%>
    <%
        
        var dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;

        foreach (var item in Model.OrderBy(a => a.Year).ThenBy(a => a.Month))
        {
            var monthNames = dateTimeFormat.MonthNames[item.Month - 1].Capitalize();
            var nbDay = DateTime.DaysInMonth(item.Year, item.Month);
            var holidays = WorkingDay.MonthWorkingDays(item.Year, item.Month).ToArray();
            var dateLine = new StringBuilder();

            for (var i = 1; i <= nbDay; i++)
            {
                dateLine.AppendFormat("<th class=\"CellDayHeader\">{0}</th>", i);
            }

            Response.Write("<table class=\"CramGrid\">");
            Response.Write(string.Format("<tr><th class=\"CellCramTitleHeader\" ><h3>{0} {1}</h3></th>{2}<th class=\"CellDayHeader\"/></tr>", monthNames, item.Year, dateLine));

            foreach (var activity in item.ActivitiesModel.OrderBy(a => a.Activity.Name))
            {
                Response.Write(string.Format("<tr><td colspan='{2}'><b>{0} : </b></td><td class=\"ReadonlyCellDay\"><b>{1}</b></td><td /></tr>", activity.Activity.Name, activity.Total.ToString("0.#"), nbDay + 1));

                foreach (var user in activity.UsersModel.OrderBy(u => u.User.LastName).ThenBy(u => u.User.FirstName))
                {
                    var image = user.Line.Status == (int)CramStatus.Validated
                                    ? "~/Content/Images/approbation.png"
                                    : "~/Content/Images/ok.png";

                    var tooltip = user.Line.Status == (int)CramStatus.Validated
                                    ? "Cram validé - à approuver"
                                    : "Cram soumis - à valider";

                    Response.Write(string.Format("<tr><td>{0} {1}</td>{2}<td class=\"ReadonlyCellDay\">{3}</td><td>{4} {5}</td></tr>",
                        user.User.LastName.ToUpper(), user.User.FirstName,
                        string.Join("", user.Line.Rates.Select(da => string.Format("<td class=\"{1}\">{0}</td>", da.Value != 0 ? da.Value.ToString("0.#") : "", !holidays[da.Key - 1] ? "ReadonlyCellHoliday" : "ReadonlyCellDay"))),
                        user.Line.Rates.Sum(r => r.Value).ToString("0.#"),
                        Html.Image("ValidateCramLine", image, tooltip, "CramList", string.Format("form['cramLineId'].value ='{0}';", user.Id)),
                        Html.Image("EditByCramLineId", "~/Content/Images/Edit.png", "Modifier", "CramList", string.Format("form['cramLineId'].value ='{0}';", user.Id))));
                }

                Response.Write("<tr />");
            }

            Response.Write("</table>");
            Response.Write("<br /><br />");
        }
    %>
    </form>
</asp:Content>
